#!/bin/bash
cd ~/app-root/data/temp/firefox/
base="https://ftp.mozilla.org/pub/mobile/candidates"
getLatest(){
    sver="$1" #52.0或52.0esr或52.0b10
    ver="$sver-candidates"
    build=$(curl -s -kL $base/$ver/|grep -o "build[1-9]"|sort -u|tail -1)
    if ! grep -q $ver/$build/android'.*'zh-CN ~/app-root/data/index/firefox.html; then
        if curl -s -kL $base/$ver/$build/android-api-15/zh-CN/ |grep -q "fennec.*\.apk"; then
            if echo $ver |grep -q b; then
                sed -i -e "/android be/s#[0-9]\{2\}\.[0-9b]\{3,4\}#$sver#g" -e "/android be/s#build[0-9]\{1,2\}#$build#g" -e "/android be/s#multi#zh-CN#g" -e "/android be/s#多语#简中#" ~/app-root/data/index/firefox.html
            else
                sed -i -e "/android re/s#[0-9]\{2\}\.[0-9]\{0,1\}\.\{0,1\}[0-9]\{1\}#$sver#g" -e "/android re/s#build[0-9]\{1,2\}#$build#g" -e "/android re/s#multi#zh-CN#g" -e "/android re/s#多语#简中#" ~/app-root/data/index/firefox.html
            fi 
            toup=1
        elif ! grep -q $ver/$build/android'.*'multi ~/app-root/data/index/firefox.html; then
            if curl -s -kL $base/$ver/$build/android-api-15/multi/ |grep -q "fennec.*\.apk"; then
                if echo $ver |grep -q b; then
                    sed -i -e "/android be/s#[0-9]\{2\}\.[0-9b]\{3,4\}#$sver#g" -e "/android be/s#build[0-9]\{1,2\}#$build#g" -e "/android be/s#zh-CN#multi#g" -e "/android be/s#简中#多语#" ~/app-root/data/index/firefox.html
                else
                sed -i -e "/android re/s#[0-9]\{2\}\.[0-9]\{0,1\}\.\{0,1\}[0-9]\{1\}#$sver#g" -e "/android re/s#build[0-9]\{1,2\}#$build#g" -e "/android re/s#zh-CN#multi#g" -e "/android re/s#简中#多语#" ~/app-root/data/index/firefox.html
            fi 
            toup=1
        fi
    fi
fi
}

curl -s -kL $base/ -o firefox.html
cp -f ~/app-root/data/index/firefox.html ~/app-root/data/temp/firefox/firefox1.html
#beta
latest=$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}b[0-9]\{1,2\}-candidates" firefox.html|grep -o '^[0-9]\{2\}'|tail -1)
getLatest "$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}b[0-9]\{1,2\}-candidates" firefox.html|sort -u|grep -o "$latest[0-9b\.]*"|tail -11|sort -t 'b' -k2n|tail -1)"
#release
getLatest "$(grep -o "[0-9]\{2\}\.[0-9\.]*[0-9]\{1\}-candidates" firefox.html|grep -o '[0-9\.]*'|tail -1)"

if ! diff -q ~/app-root/data/index/firefox.html ~/app-root/data/temp/firefox/firefox1.html; then
    sh ~/app-root/data/github-sh/firefox/time.sh
    cd ~/app-root/data/index/
    git add --all
    git commit -m "update"
    git push -u origin gh-pages
fi
rm -f ~/app-root/data/temp/firefox/firefox.html ~/app-root/data/temp/firefox/firefox1.html
